<?php

namespace App\Http\Controllers\Api;

use App\Model\bookItem;
use App\Model\bookTag;
use App\Model\Chapter;
use App\Model\CollectionItems;
use App\Model\Comment;
use App\Model\NodealChapters;
use App\Model\NodealOriginal;
use App\Model\OriginalBook;
use App\Model\OriginalChapters;
use App\Model\Userlibrary;
use App\Model\UserManage;
use Illuminate\Http\Request;
use DB;

class ContentController extends CommonController
{
    //(1)多表关联，由tag_id获取book_id对应的book_item数据
    public function book($tid)
    {
//        有tid则获取对应tid的条目，没有则获取全部条目
        if($tid) {
            $data = DB::table('book_items')
//                     由于book_items表和tag_book表都有id值，死了防止混淆，我们设置只取book_item的所有数据
                     ->select('book_items.*')
                     ->join('tag_books','book_items.id','=','tag_books.book_id')
                     ->where('tag_id',$tid)
                     ->get();
        } else {
            $data = bookItem::get();
        }
        return $this->response($data);
    }

    //(2)多表关联，由book_id获取对应tag_id的tag_item数据
    public function tag($bookid)
    {
//        有tid则获取对应tid的条目，没有则获取全部条目
        if($bookid) {
            $data = DB::table('book_tags')
//              由于book_items表和tag_book表都有id值，死了防止混淆，我们设置只取book_item的所有数据
                ->select('book_tags.*')
                ->join('tag_books','book_tags.id','=','tag_books.tag_id')
                ->where('book_id',$bookid)
                ->get();
        } else {
            $data = bookTag::get();
        }
        return $this->response($data);
    }

    //(3)多表关联，收藏列表功能，由user_id获取对应book_id的book_item数据
    public function collection($userid)
    {
//        有uid则获取对应uid的条目，没有则获取全部条目
        if($userid) {
            $data = DB::table('book_items')
//              由于book_items表和userlibraries表都有id值，死了防止混淆，我们设置只取book_item的所有数据
                ->select('book_items.*')
                ->join('collection_items','book_items.id','=','collection_items.book_id')
                ->where('user_id',$userid)
                ->get();
        } else {
            $data = bookItem::get();
        }
        return $this->response($data);
    }

    //(4)获取该模型下的数据,测试
    public function bookitem()
    {
        return $this->response(bookItem::get());

    }
    //(5)根据id值获取该模型下的某条数据,测试
    public function bookitem_query($book_id)
    {
//        这里的id值一定要和数据库里的id字段名一致！！！！
        $data = bookItem::where('id',$book_id)->get();
        return $this->response($data);

    }

    //(6)根据id值获取该模型下的某条数据,测试
    public function chapters($bookId)
    {
        $data = Chapter::where('book_id',$bookId)->get();

        return $this->response($data);
    }

//    (7)根据章节id获取小说文件，展示在阅读页面
    public function chapters_query($chapter_id)
    {
//        这里的id值一定要和数据库里的id字段名一致！！！！
        $data = Chapter::where('id',$chapter_id)->get();
        return $this->response($data);

    }

    // (8)获取书本标签（类型）表的所有标签名
    public function book_tag()
    {
//        获取bookTag表数据
        return $this->response(bookTag::get());

    }

    // (9)获取书本标签（类型）表的所有标签名
    public function login_query($username)
    {
//        这里的id值一定要和数据库里的id字段名一致！！！！
        $data = Userlibrary::where('username',$username)->get();
        return $this->response($data);
    }

    // (10)获取用户表的所有个人信息
    public function user_query($userid)
    {
//        这里的id值一定要和数据库里的id字段名一致！！！！
        $data = Userlibrary::where('id',$userid)->get();
        return $this->response($data);
    }

    //(11)获取该模型下的数据
    public function loginquery()
    {
        return $this->response(UserManage::get());

    }

    //(12)获取该模型下的数据
    public function originals()
    {
        return $this->response(OriginalBook::get());
    }

    //(13)按专区名搜索，获取用户原创作品表信息
    public function original_query($special_area)
    {
//        这里的id值一定要和数据库里的id字段名一致！！！！
        $data = OriginalBook::where('special_area',$special_area)->get();
        return $this->response($data);
    }

//    (14)判断某书是否被某人收藏
    public function collection_check(Request $request)
    {
//        调用父类控制器中的响应方法
        $data_id = $request->ubID;
        $result = DB::table('collection_items')->where('ub_id', '=', $data_id)->get();

        return $result;
    }

    //(15)点击收藏按钮保存数据
    public function addcollection(Request $request)
    {
//        调用父类控制器中的响应方法
        $collectionItems = new CollectionItems();

        $user_id = $request->userID;
        $book_id = $request->bookID;
        $ub_id = $request->ubID;

        $collectionItems->user_id = $user_id;
        $collectionItems->book_id = $book_id;
        $collectionItems->ub_id = $ub_id;

        $collectionItems->save();

        return "收藏成功";
    }

    //(16)点击收藏按钮删除数据
    public function delcollection(Request $request)
    {
//        调用父类控制器中的响应方法
        $ub_id = $request->ubID;

        DB::table('collection_items')-> where('ub_id', $ub_id)->delete();

        return "取消收藏成功";
    }

    //(17)根据bookid值获取评论表下的对应评论数据
    public function comment($bookId)
    {
        $data = Comment::where('book_id',$bookId)->get();

        return $this->response($data);
    }

    //(17)作品评论的数据保存
    public function comment_preservation(Request $request)
    {
        //调用父类控制器中的响应方法
        $comment = new Comment();

        $comment_user = $request->comment_user;
        $comment_content = $request->comment_content;
        $book_id = $request->book_id;
        $approval_id = 0; //表示该条评论未经过审核

        $comment->comment_user = $comment_user;
        $comment->comment_content = $comment_content;
        $comment->book_id = $book_id;
        $comment->approval_id = $approval_id;

        $comment->save();

        return "发送成功";
    }

    //(18)原创作品上传
    public function upload_OriginalBook(Request $request)
    {
        // 调用父类控制器中的响应方法
        $nodealoriginal = new NodealOriginal();

        $book_name = $request->book_name;
        $author = $request->author;
        $special_area = $request->special_area;
        $book_type = $request->book_type;
        $introduce = $request->introduce;

        $nodealoriginal->book_name = $book_name;
        $nodealoriginal->author = $author;
        $nodealoriginal->special_area = $special_area;
        $nodealoriginal->book_type = $book_type;
        $nodealoriginal->introduce = $introduce;

//      未审核作品数值加1
        DB::table('userlibraries')->where('username',$author)->increment('Noaudit_original',1);

        $nodealoriginal->save();

        $id = $nodealoriginal->id;

        return $id;
    }

    //(19)原创作品章节上传
    public function upload_OriginalChapter(Request $request)
    {
        //        调用父类控制器中的响应方法
        $nodealchapters = new NodealChapters();

        $chapter_title = $request->chapter_title;
        $chapter_path = $request->chapter_path;
        $book_id = $request->book_id;


        $nodealchapters->chapter_title = $chapter_title;
        $nodealchapters->chapter_path = $chapter_path;
        $nodealchapters->book_id = $book_id;

        $nodealchapters->save();

        return '作品已上传成功，请等待审核';
    }

    //(20)原创作品根据id查询
    public function OriginalBook_query($original_id)
    {
        // 这里的id值一定要和数据库里的id字段名一致！！！！
        $data = OriginalBook::where('id',$original_id)->get();
        return $this->response($data);
    }

    //(21)根据id值获取该模型下的某条数据
    public function OriginalBook_chapters($original_id)
    {
        $data = OriginalChapters::where('book_id',$original_id)->get();

        return $this->response($data);
    }

    //(22)按专区名称查询书本items
    public function special_name($special_name)
    {
        $data = OriginalBook::where('special_area',$special_name)->get();

        return $this->response($data);
    }

    //(23)根据原创作品的章节id获取小说文件，展示在阅读页面
    public function Original_read($chapter_id)
    {
//        这里的id值一定要和数据库里的id字段名一致！！！！
        $data = OriginalChapters::where('id',$chapter_id)->get();
        return $this->response($data);

    }

    //(24)获取未审核作品表中的数据
    public function nodealOriginals()
    {
        return $this->response(NodealOriginal::get());

    }

    //(25)根据book_id值获取该模型下的某条数据,测试
    public function nodealOriginals_chapter($bookId)
    {
        $data = NodealChapters::where('book_id',$bookId)->get();

        return $this->response($data);
    }

    //(26)原创作品审核-通过(审核通过后先保存及后删除)
    public function nodealOriginals_ApprovalAdopt(Request $request)
    {
        //先保存（操作原创作品表）
        $originalBook = new OriginalBook();

        $book_name = $request->book_name;
        $author = $request->author;
        $special_area = $request->special_area;
        $book_type = $request->book_type;
        $introduce = $request->introduce;
        $bookId = $request->bookid;

        $originalBook->book_name = $book_name;
        $originalBook->author = $author;
        $originalBook->special_area = $special_area;
        $originalBook->book_type = $book_type;
        $originalBook->introduce = $introduce;

//        该用户的原创作品数量加1,未审核作品减1
        DB::table('userlibraries')->where('username',$author)->increment('original_book',1);
        DB::table('userlibraries')->where('username',$author)->decrement('Noaudit_original',1);

        $aa=array();

        $originalBook->save();
        $id = $originalBook->id;

        //后删除（操作未审核作品表）
        $bookid = $request->bookid;
        DB::table('nodeal_originals')-> where('id', $bookid)->delete();
        array_push($aa,$id);

//      把该书下对应的章节存进原创作品章节表
        $data = NodealChapters::where('book_id',$bookId)->get();
        array_push($aa,$data);

        return $aa;
    }

    //(25)把未审核作品章节信息存进原创作品章节表
    public function nodealChapter_del(Request $request)
    {
        // 先把章节信息存进原创作品章节表
        //先保存（操作原创作品表）
        $originalChapters = new OriginalChapters();

        $chapter_path = $request->chapter_path;
        $chapter_title = $request->chapter_title;
        $book_id = $request->book_id;
        $chapter_id = $request->id;

        $originalChapters->chapter_path = $chapter_path;
        $originalChapters->chapter_title = $chapter_title;
        $originalChapters->book_id = $book_id;

        $originalChapters->save();

//      再把未审核作品章节表中的对应章节信息删除
        DB::table('nodeal_chapters')-> where('id', $chapter_id)->delete();

        return '操作成功';
    }


    //(26)原创作品审核-拒绝(审核拒绝后删除该条数据)
    public function nodealOriginals_ApprovalRefuse(Request $request)
    {
        //后删除（操作未审核作品表）
        $bookid = $request->bookid;
        $author = $request->author;

//      未审核作品减1
        DB::table('userlibraries')->where('username',$author)->decrement('Noaudit_original',1);

//      删除该本书籍，以及其下的所有章节
        DB::table('nodeal_originals')-> where('id', $bookid)->delete();
        DB::table('nodeal_chapters')-> where('book_id', $bookid)->delete();

        return '操作成功';
    }

    //(27)加载所有未经审核的评论
    public function load_comment()
    {
        $data = Comment::where('approval_id',0)->get();

        return $data;
    }

    //(28)评论审核-通过(评论通过后给对应id条目的数据审核id赋值为1)
    public function comment_ApprovalAdopt($comment_id)
    {
        // 调用父类控制器中的响应方法
        $comment = Comment::where('id',$comment_id)->update(['approval_id'=>1]);

        return '审核成功';
    }

    //(29)评论审核-通过(评论通过后给对应id条目的数据审核id赋值为1)
    public function comment_ApprovalRefuse($comment_id)
    {
        // 调用父类控制器中的响应方法
        $comment = Comment::where('id',$comment_id)->delete();

        return '审核成功';
    }

    //(30)未审核作品根据id查询
    public function nodealBook_query($nodel_id)
    {
        // 这里的id值一定要和数据库里的id字段名一致！！！！
        $data = NodealOriginal::where('id',$nodel_id)->get();
        return $this->response($data);
    }

    //(31)根据id值获取该模型下的某条数据
    public function nodealBook_chapters($nodel_id)
    {
        $data = NodealChapters::where('book_id',$nodel_id)->get();

        return $this->response($data);
    }

    //(32)根据章节id获取未审核小说文件，展示在阅读页面
    public function nodeal_query($chapter_id)
    {
//        这里的id值一定要和数据库里的id字段名一致！！！！
        $data = NodealChapters::where('id',$chapter_id)->get();

        return $this->response($data);
    }

    //（33）读取txt文件信息(小说书籍)
    public function noveltxt_read($name)
    {
         $filename = 'http://novel-read.oss-cn-shenzhen.aliyuncs.com/book_file/'.$name.'.txt'; //先读取文件

        $content=fopen($filename,'r');
        while(!feof($content)){
            $str=fgets($content);
            $encoding = mb_detect_encoding($str,array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
            if($encoding!=false){
                $str = iconv($encoding,'UTF-8',$str);
                echo $str.'<br>';
            }else{
                $str = mb_convert_encoding($str,'UTF-8','Unicode');
                echo $str.'<br>';
            }
        }
    }

    //（33）读取txt文件信息(小说书籍)
    public function originaltxt_read($name)
    {
        $filename = 'http://novel-read.oss-cn-shenzhen.aliyuncs.com/user_file/'.$name.'.txt'; //先读取文件

        $content=fopen($filename,'r');
        while(!feof($content)){
            $str=fgets($content);
            $encoding = mb_detect_encoding($str,array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
            if($encoding!=false){
                $str = iconv($encoding,'UTF-8',$str);
                echo $str.'<br>';
            }else{
                $str = mb_convert_encoding($str,'UTF-8','Unicode');
                echo $str.'<br>';
            }
        }
    }

    //（33）读取txt文件信息(小说书籍)
    public function nodealtxt_read($name)
    {
        $filename = 'http://novel-read.oss-cn-shenzhen.aliyuncs.com/noDeal_Original/'.$name; //先读取文件

        $content=fopen($filename,'r');
        while(!feof($content)){
            $str=fgets($content);
            $encoding = mb_detect_encoding($str,array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
            if($encoding!=false){
                $str = iconv($encoding,'UTF-8',$str);
                echo $str.'<br>';
            }else{
                $str = mb_convert_encoding($str,'UTF-8','Unicode');
                echo $str.'<br>';
            }
        }
    }
}
